package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCategory;
import com.qf.service.DtsCategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCategory category){

        return categoryService.delete(category);

    }

    @RequestMapping("/create")
    public Object create(@RequestBody DtsCategory category){
        return categoryService.insert(category);
    }

    @RequestMapping("/update")
    public Object update(@RequestBody DtsCategory category){

        return  categoryService.update(category);

    }

    @RequestMapping("/l1")
    public Object l1(){

        List<CatVo> catVos = categoryService.findCategoryL1();

        return ResponseUtil.ok(catVos);
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "limit", required = false, defaultValue = "20") Integer limit,
                       Integer id,
                       String name,
                       @RequestParam(name = "sort", required = false, defaultValue = "add_time") String sort,
                       @RequestParam(name = "order", required = false, defaultValue = "desc") String order){

        IPage<DtsCategory> iPage = categoryService.queryPage(page, limit, id, name, sort, order);

        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());

        return ResponseUtil.ok(data);
    }
}
